﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using PerfAssist.Common.Editor;
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR

#endif

namespace PerfAssist.MemoryProfilerAdvanced.Editor
{
    public class ShaderComponnentScanner
    {
#if UNITY_EDITOR
        [MenuItem(PAEditorConst.DevCommandPath + "/ShaderPropertyNameScanner")]
        private static void ScanerShaderObj()
        {
            var allAssets = Resources.LoadAll("");

            var scanerResultDict = new Dictionary<string, string>();
            foreach (var asset in allAssets)
            {
                var shader = asset as Shader;
                if (shader != null)
                {
                    var propertyNameSB = new StringBuilder();
                    var propertyCount = ShaderUtil.GetPropertyCount(shader);
                    for (var i = 0; i < propertyCount; ++i)
                        if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv)
                        {
                            var propertyName = ShaderUtil.GetPropertyName(shader, i);
                            if (!string.IsNullOrEmpty(propertyName))
                            {
                                if (!string.IsNullOrEmpty(propertyNameSB.ToString()))
                                    propertyNameSB.Append(ResourceTrackerConst.shaderPropertyNameJsonToken);
                                propertyNameSB.Append(propertyName);
                            }
                        }

                    if (!scanerResultDict.ContainsKey(shader.name))
                        scanerResultDict.Add(shader.name, propertyNameSB.ToString());
                }
            }

            var shaderPropertyDict = new Dictionary<string, string>();
            foreach (var pair in scanerResultDict) shaderPropertyDict.Add(pair.Key, pair.Value);

            try
            {
                var memConfigPath = Path.Combine(Application.persistentDataPath, "TestTools");
                if (!Directory.Exists(memConfigPath))
                    Directory.CreateDirectory(memConfigPath);

                var memResourceTrackerPath = Path.Combine(memConfigPath, "ResourceTracker");
                if (!Directory.Exists(memResourceTrackerPath))
                    Directory.CreateDirectory(memResourceTrackerPath);

                StreamWriter sw;
                var fileInfo = new FileInfo(ResourceTrackerConst.shaderPropertyNameJsonPath);
                sw = fileInfo.CreateText();
                sw.Write(JsonUtility.ToJson(new Serialization<string, string>(shaderPropertyDict)));
                sw.Close();
                sw.Dispose();
                Debug.Log("write shaderProperty Json successed");
                EditorUtility.DisplayDialog("write shaderProperty Json successed", "write shaderProperty Json successed",
                    "确认");
            }
            catch (Exception ex)
            {
                Debug.LogErrorFormat("write shaderProperty json file error,errMsg = {0}", ex.Message);
                EditorUtility.DisplayDialog("write shaderProperty Json failed",
                    string.Format("write shaderProperty Json failed ,errMsg = {0}", ex.Message), "确认");
            }
        }
#endif
    }
}